
#foreach($entity in $Neo.Model.Entities)
#foreach($rel in $entity.ToOneRelationships)
IF EXISTS(SELECT * FROM dbo.sysobjects WHERE id = object_id(N'fk_${entity.TableName}_${rel.VarName}') AND OBJECTPROPERTY(id, N'IsConstraint') = 1)
	ALTER TABLE $entity.TableName DROP CONSTRAINT fk_${entity.TableName}_${rel.VarName};

#end
#end

#foreach($entity in $Neo.Model.Entities)
IF EXISTS(SELECT * FROM dbo.sysobjects WHERE id = object_id(N'$entity.TableName') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
	DROP TABLE $entity.TableName

#end

#foreach($entity in $Neo.Model.Entities)
CREATE TABLE $entity.TableName
(
#foreach($attribute in $entity.Attributes)
	$attribute.ColumnName $attribute.ColumnType#if($attribute.Size)($attribute.Size)#end#if($attribute.IsPkColumn && $entity.IdMethod.ToString == "Native") IDENTITY#end#if($attribute.DefaultValue) DEFAULT('$attribute.DefaultValue')#end#if(!$attribute.AllowsNull) NOT NULL#end,
#end

	CONSTRAINT pk_$entity.TableName PRIMARY KEY ( #foreach($column in $entity.PkColumns)#if( $velocityCount > 1), #end$column.ColumnName#end )
);

#end

#foreach($entity in $Neo.Model.Entities)
#if($entity.ToOneRelationships.Count > 0)
#foreach($rel in $entity.ToOneRelationships)
ALTER TABLE $entity.TableName ADD CONSTRAINT fk_${entity.TableName}_${rel.VarName} FOREIGN KEY ( $rel.LocalKey ) REFERENCES $rel.ForeignEntity.TableName ( $rel.ForeignKey ); 
#end

#end
#end
